var modulePruviewModule = angular.module('modulePruviewModule', ['paginationService', 'anajaxService','agularValidate','innerPaginationService']);

modulePruviewModule.controller('ModulePurviewController', function($scope, $http,pagination,anajax,innerPagination) {
	
	$scope.moduleInfoForm={};
	$scope.resultList={};
	$scope.moduleMenuForm={};
	$scope.modulePurviewForm={};
	$scope.modulePurviewForm.moduleMenuId="";
	$scope.isLastMenu=false;
	$scope.modules={};
	
	
	
	/**
	 * 查询模块
	 */
	var getModules = function() {
		var url = commonutil.actionPath + '/module/purview/modules';
		anajax.doajax(url, null, function(data) {
			$scope.modules=data;
		});
	};
	
	getModules();
	
	
	var setting={
			callback: {
				onClick:  zTreeOnClick
			},
			data:{
			key:{
			name:"menuName",
			title:"menuName"
			},
			simpleData:{
			enable:true,
			idKey:"moduleMenuId",
			pIdKey:"parentId"
			}
			}
		};
	
	
	//ztree 树形菜单节点点击事件
	function zTreeOnClick(event, treeId, treeNode){
		 
		 $scope.moduleMenuForm.moduleMenuId=treeNode.moduleMenuId;
		 $scope.modulePurviewForm.moduleMenuId=treeNode.moduleMenuId;
		 
		var treeObj = $.fn.zTree.getZTreeObj("moduleMenuTree");
		var sNodes = treeObj.getSelectedNodes();
		
		console.log(sNodes);
		if (sNodes.length > 0) {
			var level = sNodes[0].level;
			if(sNodes[0]!=null&&sNodes[0].children!=null&&sNodes[0].children.length!=null&&sNodes[0].children.length>0){
				$scope.isLastMenu=false;
			}else{
				$scope.isLastMenu=true;
			}
			
		}

		/*重新加载表格*/
		query($scope.moduleMenuForm);
		}
	 
	/**
	 * 点击查询
	 */
	$scope.doquery = function() {
		
		query($scope.moduleMenuForm);
	};

	 /**
		 * 查询
		 */
		var query = function(params) {
			var url = commonutil.actionPath + '/module/purview/purviewlist';
			anajax.doajax(url, params, function(data) {
				$scope.resultList = data.resultList;
				pagination.setData(data.pagination);
				$scope.pagination = data.pagination; 
			});
		};
		
	
	$scope.changeModule=function(){
		var id=$scope.selected;
		if(id==0){
			return false;
		}
		$scope.moduleMenuForm.moduleMenuId=-1;
		query($scope.moduleMenuForm);
		$scope.moduleInfoForm.moduleInfoId=id;
		$("#moduleMenuTree").innerHTML="";
		var url = commonutil.actionPath + '/module/purview/findModule';
		anajax.doajax(url, $scope.moduleInfoForm, function(data) {
			var treeNodes =data;
			 $.fn.zTree.init($("#moduleMenuTree"), setting, treeNodes);
			 var treeObj = $.fn.zTree.getZTreeObj("moduleMenuTree");
			 treeObj.expandAll(true);
			
		});
		
	}
	
	/**初始化新增*/
	$scope.goAdd = function(index) {
		var moduleMenuId= $scope.modulePurviewForm.moduleMenuId;
		var iscontinue=false;
		if(!$scope.isLastMenu){
			iscontinue=true;
		}
		
		if(moduleMenuId==null||moduleMenuId==""){
			iscontinue=true;
			
		}
		if(iscontinue){
			pageutil.showTip('请选择子菜单');
			return false;
		}
		$scope.modulePurviewForm = {
		};
		$scope.modulePurviewForm.moduleMenuId=moduleMenuId;
		$scope.stations = [];
		$scope.rightPart = 'add';
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('新增权限');
	};
	
	
/**初始化编辑*/
	$scope.goModify  = function(index) {
		$scope.rightPart = 'edit';
		var assets=$scope.resultList[index];
		angular.copy(assets,$scope.modulePurviewForm);
		$scope.modulePurviewForm.createTime=null;
		$scope.modulePurviewForm.lastUpdateTime=null;
		removeTips($('#edit-data-warp'));
		pageutil.showRightBarAn('编辑权限');
	};
	
	$scope.doCancel = function(){
		pageutil.hideRightBar();
	}
	
	/**
	 * 弹出确认删除窗口
	 */
	$scope.showDelWin = function($index){
		var assetBean = $scope.resultList[$index];
		$scope.delBean= assetBean;
		//$scope.delBean.useTime=null;
		$("#confirm_del_asset_div").modal('show');
	};
	
	$scope.doDelAsset = function() {
	    console.log($scope.delBean);
		var url = commonutil.actionPath + '/module/purview/del';
		anajax.doajax(url,{id:$scope.delBean.modulePurviewId}, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('删除成功');
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	/**
	 * 执行添加
	 */
	$scope.doAdd = function() {
		
		var res = Validator.validate($("#edit-data-warp"));
		if(!res){
			return;
		}
		if($scope.rightPart=='add'){
			$scope.add($scope.modulePurviewForm);
		}else{
			$scope.modify($scope.modulePurviewForm);
		}
	};
	
	
	/**
	 * 执行添加
	 */
	$scope.add = function(postData) {
		$scope.modulePurviewForm.moduleMenuId=$scope.moduleMenuForm.moduleMenuId;
		var url = commonutil.actionPath + '/module/purview/add';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('添加成功');
				pageutil.hideRightBar();
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	/**
	 * 执行修改
	 */
	$scope.modify = function(postData) {
		
		var url = commonutil.actionPath + '/module/purview/update';
		anajax.doajax(url, postData, function(data) {
			if (data.resultValue ) {
				pageutil.showTip('修改成功');
				pageutil.hideRightBar();
				$scope.doquery();
			} else {
				pageutil.showTip(data.message);
			}
		});
	};
	
	$scope.delBean ={};
	
	
	/**
	 * 跳转到指定页
	 */
	$scope.goPage = function(selNum) {
		
		pagination.goPage(selNum);
		query($scope.pagination);
	};
	
	/**
	 * 上一页
	 */
	$scope.lastPage = function() {
		pagination.lastPage();
		query($scope.pagination);
	};
	
	/**
	 * 下一页
	 */
	$scope.nextPage = function() {
		pagination.nextPage();
		query($scope.pagination);
	};
});